package br.com.iswe.gwt.ui.client;

import br.com.iswe.gwt.util.client.ClientUtils;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.WindowResizeListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.PopupPanel;

public class ModalPopupPanel extends PopupPanel {
	
	private static final String _100_PORCENTO = "100%";

	private static final String ISWE_MODALPANEL = "iswe-modalpanel";

	private static final String PX = "px";

	protected FlexTable internalTable = new FlexTable();
	
	protected PopupPanel backGroundAlphaPanel = new PopupPanel();
	
	
	protected boolean disableScrolling = true;
	
	protected void resizeBackGround(){
		//Posicao
		int baseTop = Window.getScrollTop() + 20;
		int baseLeft = Window.getScrollLeft() + 20;
		//configura o popup com o tamanho inteiro da tela
		String width = (Window.getClientWidth() + baseLeft) + PX;
		String height = (Window.getClientHeight() + baseTop) + PX;
		backGroundAlphaPanel.setSize(width, height);
		backGroundAlphaPanel.setPopupPosition(0, 0);
	}
	
	public void show() {
		//Mostra o BackGround
		backGroundAlphaPanel.show();
		resizeBackGround();
		if(isDisableScrolling()){
			Window.enableScrolling(false);
		}
		super.show();
	}
	
	public void hide() {
		//Esconde o BackGround
		backGroundAlphaPanel.hide();
		Window.enableScrolling(true);
		super.hide();
	}
	
	public ModalPopupPanel(){
		this(false);
	}
	
	public ModalPopupPanel(boolean autoHide){
		super(autoHide, true);
		//Configura o Painel que fica como BackGround em Alpha
		//seta o estilo
		backGroundAlphaPanel.setStyleName(ISWE_MODALPANEL);
		//configura a tabela interna
		internalTable.setWidth(_100_PORCENTO);
		internalTable.setHeight(_100_PORCENTO);
		internalTable.setCellPadding(0);
		internalTable.setCellSpacing(0);
		internalTable.setWidget(0, 0, ClientUtils.buildSpace(10));
		backGroundAlphaPanel.setWidget(internalTable);
		
		Window.addWindowResizeListener(new WindowResizeListener(){
			public void onWindowResized(int width, int height) {
				resizeBackGround();
			}
		});
	}

	public boolean isDisableScrolling() {
		return disableScrolling;
	}

	public void setDisableScrolling(boolean disableScrolling) {
		this.disableScrolling = disableScrolling;
	}
}